
package pos.control;

import java.lang.Double;
import java.math.BigDecimal;
import java.math.BigInteger;
import pos.Pos;
import pos.entity.Item;
import pos.storage.db.mysql.SelectQueryTableModel;
import pos.storage.db.sql.SQL;

/**
 * A control object that queries the item list and populates it into a TableModel
 * @author rsrikanth
 */
public class ItemViewControl {

    /**
     * Executes the input SQL and populates the results into the Table model
     * @param sTableModel
     * @param sql 
     */
    public void genTable(SelectQueryTableModel sTableModel, String sql){
        try{
            if(sql != null){
                //System.out.println("==== " + sql);
                sTableModel.genTable(Pos.adminConn, sql);
            }
            else{
                sTableModel.genTable(Pos.adminConn, SQL.SELECTITEM);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
    }
    public BigDecimal getBill(SelectQueryTableModel sTableModel){
        int ccount = sTableModel.getColumnCount();
        int colPos = 0;
        for(int i=0; i<ccount; i++){
            if("Total".equalsIgnoreCase(sTableModel.getColumnName(i))){
                colPos = i;
            }
        }
        int rcount = sTableModel.getRowCount();
        BigDecimal bd = new BigDecimal(0d);
        for(int i=0; i<rcount; i++){
            Double obj = (Double) sTableModel.getValueAt(i, colPos);
            if(obj != null){
                bd = bd.add(new BigDecimal(obj));
            }
        }
        
        return bd;
    }
}
